<?php
/**
 *	@author wyl
 *  @date 2015-8-24 15:16:49
 */
class AuctionStatusModify{
	const AUCTION_STATUS_UNCOMING=0;
	const AUCTION_STATUS_LIVE=1;
	const AUCTION_STATUS_END=2;
	
	public $liveAuctions=array();
	public $endAuctions=array();
	
	public function ModifyStatus(){
		$this->getLiveGroup();
		if(!empty($this->liveAuctions)){
			foreach ($this->liveAuctions as $live){
				$this->toLive($live);
			}
		}
		
		$this->getEndGroup();
		if(!empty($this->endAuctions)){
			foreach ($this->endAuctions as $end){
				$this->toEnd($end);
			}
		}
	}
	
	public function getLiveGroup(){
		$model=new AuctionPool();
		$this->liveAuctions=array();
		$criteria=new CDbCriteria();
		$criteria->addCondition('start_time<='.time());
		$criteria->addCondition('end_time>='.time());
		$criteria->addCondition('status=0');
		$uncomingAuctions=$model->findAll($criteria);
		foreach ($uncomingAuctions as $uncoming){
			if(time()>=$uncoming->start_time && time()<=$uncoming->end_time){
				$dao=new AuctionPool();
				$dao=$dao->findByPk($uncoming->auction_id);
				$this->liveAuctions[]=$dao;
			}
		}
	}
	
	public function getEndGroup(){
		$model=new AuctionPool();
		$this->endAuctions=array();
		$criteria=new CDbCriteria();
		$criteria->addCondition('status=1');
		$criteria->addCondition('end_time<'.time());
		$liveAuctions=$model->findAll($criteria);
		foreach ($liveAuctions as $live){
			if(time()>$live->end_time){
				$dao=new AuctionPool();
				$dao=$dao->findByPk($live->auction_id);
				$this->endAuctions[]=$dao;
			}
		}
	}
	
	
	public function toLive($auction){
		if($auction instanceof AuctionPool){
			$auction->status=self::AUCTION_STATUS_LIVE;
			$auction->save();
		}else{
			$dao=new AuctionPool();
			$dao=$dao->findByPk($auction);
			if($dao->status!=self::AUCTION_STATUS_END && $dao->status!=self::AUCTION_STATUS_LIVE){
				$dao->status=self::AUCTION_STATUS_LIVE;
				$dao->save();
			}
		}
	}
	
	
	public function toEnd($auction){
		if($auction instanceof AuctionPool){
				$auction->status=self::AUCTION_STATUS_END;
				$auction->save();
		}else{
			$dao=new AuctionPool();
			$dao=$dao->findByPk($auction);
			if($dao->status!=self::AUCTION_STATUS_END){
				$dao->status=self::AUCTION_STATUS_END;
				$dao->save();
			}
		}
	}
	
	
}